package com.itany.shop.controller;

import com.itany.shop.entity.Order;
import com.itany.shop.entity.User;
import com.itany.shop.exception.LoginDisabledException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.OrderService;
import com.itany.shop.vo.Cart;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author:石小俊
 * @Date: 2022/12/21 9:24
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
@WebServlet("*.order")
public class OrderServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        if("/create.order".equals(path)){
            createOrder(request,response);
        }
        if("/findByUser.order".equals(path)){
            findMyOrders(request,response);
        }
        if("/findDetail.order".equals(path)){
            findDetail(request,response);
        }
    }

    public void findDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");
        String id = request.getParameter("id");

        try {
            Order order = orderService.findDetail(id);
            request.setAttribute("order",order);
            request.getRequestDispatcher("/jsp/orderDetail.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("orderMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/myOrders").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("orderMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/myOrders").forward(request,response);
        }
    }

    public void findMyOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");
        User user = (User) request.getSession().getAttribute("user");

        try {
            List<Order> orders = orderService.findMyOrders(user);
            request.setAttribute("orders",orders);
            request.getRequestDispatcher("/jsp/myOrders.jsp").forward(request,response);
        } catch (LoginDisabledException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("productMsg","服务器内部异常");
            request.getRequestDispatcher("/findAll.product").forward(request,response);
        }
    }

    public void createOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        User user = (User) request.getSession().getAttribute("user");

        try {
            String no = orderService.createOrder(cart,user);
            // 当生成订单之后,说明当前购物结束,此处购物车中数据应该被清空
            request.getSession().setAttribute("cart",new Cart());
            // 此处成功之后,使用重定向进行页面跳转
            // 因此,返回的订单号应该保存到session中
            request.getSession().setAttribute("no",no);
            response.sendRedirect(request.getContextPath()+"/jsp/createOrder.jsp");
        } catch (LoginDisabledException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("orderMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/confirm.jsp").forward(request,response);
        }
    }
}
